//
//  MZExtensionUIViewController.swift
//  MZTool
//
//  Created by gby on 2022/3/9.
//

import Foundation


extension MZKit where Base == UIViewController {
    //MARK: 关闭自身并跳转
    public func push_thenCloseSelf(to: UIViewController, ani: Bool){
        guard let navivc = base.navigationController, navivc.viewControllers.count > 0 else {
            base.navigationController?.pushViewController(to, animated: ani)
            return
        }
        let vcs = navivc.viewControllers
        var cp_vcs: [UIViewController] = vcs
        cp_vcs.removeLast()
        cp_vcs.append(to)
        navivc.setViewControllers(cp_vcs, animated: ani)
    }
    
    //MARK: 返回
    public func push_back(_ ani: Bool = true) {
        base.navigationController?.popViewController(animated: ani)
    }
    
    //MARK: 创建按钮
    public func create_BtnWithData(title: String?,
                                   font: UIFont,
                                   image: UIImage?,
                                   color: UIColor = UIColor.black,
                                   line: Int,
                                   callback: ((_ b:UIButton)->Void)?){
        
        let width = title?.mz.boundingSize(font: font, maxW: UIScreen.main.bounds.width).width
        let oneBtn = UIButton.init(type: .custom)
        do{
            oneBtn.titleLabel?.font = font
            oneBtn.frame = CGRect.init(x: 5, y: 0, width: ceil(width ?? 0.0), height: 35)
            oneBtn.titleLabel?.numberOfLines = line
            oneBtn.setTitle(title, for: .normal)
            oneBtn.setTitleColor(color, for: .normal)
            oneBtn.setTitleColor(color, for: .selected)
        }
        if image != nil {
            oneBtn.setImage(image, for: .normal)
            oneBtn.setImage(image, for: .normal)
            oneBtn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 0)
        }
        callback?(oneBtn)
    }
}
